package com.lida.study.common.utils.opencv;

import com.lida.study.common.Constants;
import com.lida.study.common.utils.tess4j.Tess4jUtils;
import com.lida.study.sudo.Sudo;
import lombok.extern.slf4j.Slf4j;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

/**
 * @author lD
 * @date 2019/7/1011:20
 */
@Slf4j
public class OpenCvUtils {

    private OpenCvUtils() {}


    /**
     * 图片处理及识别
     */
    public static Sudo openCvDeal(String filename) {
        filename = Constants.TEMP_RESOURCES_DATA_PATH + filename;
        Mat src = Imgcodecs.imread(filename);
        if (src.empty()) {
            log.error("Empty image: " + filename);
            return null;
        }
        Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
        // 阈值分割
        Imgproc.threshold(src, src, 130, 255, 0);
        Imgcodecs.imwrite(Constants.TEMP_RESOURCES_DATA_PATH + "temp.jpg", src);
        // 识别图片中的数字
        try {
            return Tess4jUtils.getWords();
        } catch (Exception e) {
            log.error("tess4j error:", e);
            return null;
        }
    }
}
